package computacaografica;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;

public class GraficoPizza {

	private int[] valores;

	private BufferedImage imageBuffer;

	private Color background;

	private int largura, altura;

	private Color[] cores = { Color.BLUE, Color.GREEN, Color.RED, Color.YELLOW };

	public GraficoPizza(int[] valores, int largura, int altura) {
		this.valores = valores;
		this.largura = largura;
		this.altura = altura;
		this.background = Color.WHITE;
		this.criaPizza();
	}

	private void criaPizza() {
		this.imageBuffer = new BufferedImage(this.largura, this.altura,
				BufferedImage.TYPE_INT_RGB);
		Graphics g = this.imageBuffer.createGraphics();
		g.setColor(this.background);
		g.fillRect(0, 0, this.largura, this.altura);
		int arco = 0;
		int[] tamanhos = this.calculaAngulos(this.valores);
		for (int i = 0; i < tamanhos.length; i++) {
			g.setColor(this.cores[i]);
			g.fillArc(0, 0, this.largura, this.altura, arco, tamanhos[i]);
			arco += tamanhos[i];
		}
	}

	private int[] calculaAngulos(int[] valores) {
		int[] angulos = new int[valores.length];
		int total = 0;
		for (int i = 0; i < valores.length; i++) {
			total += valores[i];
		}
		for (int i = 0; i < valores.length; i++) {
			angulos[i] = (360 * valores[i]) / total;
		}
		return angulos;
	}

	public BufferedImage getBuffer() {
		return this.imageBuffer;
	}

	public ImageIcon getImage() {
		return new ImageIcon(this.imageBuffer);
	}

}
